<?php
 
class MySqlDb {
	var $host;
	var $user;
	var $passwd;
	var $database;
	var $conn;
	
	/**
	* Method：构造方法
	*/
	function MySqlDb() {
		$this->host = 'localhost';
		$this->user = 'root';
		$this->passwd = '40d1c77568';
		$this->database = 'qwadmin';
		$this->prefix = '';
		
		$this->conn = mysql_connect ( $this->host, $this->user, $this->passwd ) or die ( "Could not connect" );
		
		mysql_select_db ( $this->database, $this->conn ) or die ( "Could not switch to database" );
		
		mysql_query ( "SET NAMES 'utf8'" );
	}
	
	 /**
	 * 查询方法
	 *
	 * @param string $queryStr
	 * @return string
	 */
	function query($queryStr) {
		//修改表前缀
		$queryStr = str_replace($this->prefix,'',$queryStr);
		$queryStr = str_replace('pre_',$this->prefix,$queryStr);
		
		$res = mysql_query ( $queryStr );
		//sql 错误记录
		if(!$res){
			exit("$queryStr Could not query database");
		}
		return $res;
	}
	
	 /**
	 * 返回记录集
	 *
	 * @param string $res
	 * @return array
	 */
	function getRows($res) {
		$rowno = 0;
		$rowno = MySQL_num_rows ( $res );
		if ($rowno > 0) {
			for($row = 0; $row < $rowno; $row ++) {
				$rows [$row] = mysql_fetch_array ( $res );
				//本来为MySQL_fetch_row，但是不能以数组的方式来提取，只能用索引，这样可以用索引和名称，更为方便
			}
			return $rows;
		}
	}
	
	 /**
	 * 返回数据库记录数
	 *
	 * @param string $res
	 * @return int
	 */
	function getRowsNum($res) {
		$rowno = 0;
		$rowno = mysql_num_rows ( $res );
		return $rowno;
	}

	function getAccoc($res) {
		$rowno = mysql_num_rows ( $res );
		if ($rowno > 0) {
			for($row = 0; $row < $rowno; $row ++) {
				$rows [$row] = mysql_fetch_assoc ( $res );
			}
			return $rows;
		}
	}

	 /**
	 * 关闭数据库连接
	 */
	function close() {
		mysql_close ( $this->conn );
	}
	
}

//实例化数据库类对像
if (! isset ( $conn )){
	$conn = new MySqlDb ( );
}
header("Content-type: text/html; charset=utf-8"); 

?>
